import 'package:flutter/material.dart';
import 'package:my_app/mock/goods_list.dart';
import 'package:my_app/uitls/global.dart';

class GoodsListRow extends StatelessWidget {
  final bool border;
  final String? activeDesc;
  final Function? onPress;
  final MockGoods? data;

  GoodsListRow({
    this.border = true,
    this.activeDesc,
    this.onPress,
    this.data,
  });

  Widget row(String title,
      {double fontSize = 11, FontWeight? fontWeight, Color? color}) {
    return Row(
      children: [
        Expanded(
          child: Text(
            title,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color: color ?? Color.fromRGBO(166, 166, 166, 1),
              fontWeight: fontWeight ?? FontWeight.normal,
            ),
          ),
        ),
      ],
    );
  }

  Widget goodsImg(String? imgSrc) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(4.0),
      child: Image.asset(
        imgSrc ?? '',
        width: 70,
        height: 70,
        fit: BoxFit.cover,
        errorBuilder: (context, error, stackTrace) {
          return Container(
            width: 70,
            height: 70,
            color: Colors.grey[200],
            child: Icon(Icons.image_not_supported, color: Colors.grey[400]),
          );
        },
      ),
    );
  }

  Widget activeMsg({String? text}) {
    return text == null
        ? Container()
        : Positioned(
            right: 0,
            bottom: 5,
            child: Container(
              height: 16,
              padding: EdgeInsets.symmetric(horizontal: 4),
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(2.0),
                  color: Color.fromRGBO(255, 129, 2, 1)),
              child: Text(
                text,
                style: TextStyle(color: Colors.white, fontSize: 8),
              ),
            ),
          );
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        onPress?.call(context);
      },
      child: Row(
        children: [
          Expanded(
            child: Container(
              padding: EdgeInsets.symmetric(vertical: 10),
              decoration: BoxDecoration(
                border: cuBorderBottom(show: border),
              ),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    width: 70,
                    height: 70,
                    child: Stack(
                      children: [
                        Positioned(
                          child: goodsImg(data?.pic),
                        ),
                        activeMsg(text: activeDesc)
                      ],
                    ),
                  ),
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(left: 6),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          row(data?.name ?? '',
                              color: Color.fromRGBO(56, 56, 56, 1),
                              fontSize: 15,
                              fontWeight: FontWeight.bold),
                          row(data?.characteristic?.isEmpty ?? true
                              ? ''
                              : data?.characteristic ?? ''),
                          SizedBox(height: 4),
                          Container(
                            margin: EdgeInsets.only(top: 4),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(
                                  "¥ ${data?.originalPrice ?? 0}",
                                  style: TextStyle(
                                      color: Color.fromRGBO(56, 56, 56, 1),
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold),
                                ),
                                SizedBox(
                                  width: 25,
                                  height: 25,
                                  child: Icon(
                                    Icons.add_circle,
                                    color: Color.fromRGBO(136, 175, 213, 1),
                                    size: 25,
                                  ),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
